package pack0923;

import java.util.Arrays;

public class Work01 {

    // 打印一个整型数组
    public static void a() {
        int[] array = {56, 77, 23, 59, 91, 27, 34};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + ",");
        }
        System.out.println("\b");
        System.out.println("-----------------------------");
        System.out.println(Arrays.toString(array));
        System.out.println("-----------------------------");
        // foreach 写法
        /*
            for(数据类型 迭代变量:迭代集合){
            }
         */
        for (int a : array) {
            System.out.print(a + ",");
        }
        System.out.println("\b");
    }

    // 选择排序法
    public static void b() {
        int[] array = {91, 55, 78, 23, 57, 84, 46, 88};
        System.out.println(Arrays.toString(array));
        for (int j = 0; j < array.length - 1; j++) {
            int min = array[j];
            int minIndex = j;
            for (int i = j + 1; i < array.length; i++) {
                if (min > array[i]) {
                    min = array[i];
                    minIndex = i;
                }
            }
            // 交换元素（最小值和第`一`个）
            int temp = array[j];
            array[j] = array[minIndex];
            array[minIndex] = temp;
        }
        System.out.println(Arrays.toString(array));
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + ",");
        }
    }

    // 将一个整型数组每一个元素向前（向标号小的方向）移动一个位置，第一个元素移到最后一个位置。
    public static void c1() {
        int[] array = {45, 91, 55, 78, 23, 57, 84, 46, 88};
        int temp = array[0];
        for (int i = 1; i < array.length; i++) {
            array[i - 1] = array[i];
        }
        array[array.length - 1] = temp;
        System.out.println(Arrays.toString(array));
    }

    public static void c2() {
        int[] array = {45, 91, 55, 78, 23, 57, 84, 46, 88};
        System.out.println(Arrays.toString(array));
        int temp = array[0];
        System.arraycopy(array, 1, array, 0, array.length - 1);
        array[array.length - 1] = temp;
        System.out.println(Arrays.toString(array));
    }

    // 找出前 n 个质数，以数组的形式返回结果。
    public static void d() {
        int n = (int) (Math.random() * 10 + 1);
        int[] array = new int[n];
        int i = 2;
        int index = 0;
        while (true) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                array[index] = i;
                index++;
            }
            i++;
            if (index == n) {
                break;
            }
        }
        System.out.println(Arrays.toString(array));
    }

    // 2.6 求一个整型数组各元素之和。
    public static void e() {
        int sum = 0;
        int[] array = {45, 91, 55, 78, 23, 57, 84, 46, 88};
        for (int j : array) {
            sum += j;
        }
        System.out.println(sum);
    }

    // 插入排序法
    public static void f() {
        int[] array = {45, 41, 33, 78, 23, 57, 84, 46, 88};
        System.out.println(Arrays.toString(array));
        for (int i = 1; i < array.length; i++) {
            for (int j = 0; j < i; j++) {
                if (array[i] < array[j]) {
                    int temp = array[i]; // 33
                    System.arraycopy(array, j, array, j + 1, (i - j));
                    array[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }

    // 2.11 求一个整型数组元素中最接近其平均值的整数
    public static void g() {
        double[] array = {45, 41, 33, 52, 23, 56, 78, 46, 66};
        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double avg = sum / array.length;
        System.out.println(avg);
        Arrays.sort(array);
        int begin = 0;
        int end = array.length - 1;
        int middle = (begin + end) / 2;
        while (begin < end - 1) {
            if (array[middle] > avg) {
                end = middle;
                middle = (begin + end) / 2;
            } else if (array[middle] < avg) {
                begin = middle;
                middle = (begin + end) / 2;
            } else {
                System.out.println(array[middle]);
                break;
            }
        }
        if (avg - array[begin] > array[end] - avg) {
            System.out.println(array[end]);
        } else {
            System.out.println(array[begin]);
        }
    }

    public static void main(String[] args) {
        g();
    }
}
// int temp = 42;
// 35,42,57,58